package com.parktech.sun.web.controller.management.downloadcenter;

import com.parktech.sun.domain.model.Constants;
import com.parktech.sun.domain.model.downloadcenter.ResourceType;
import com.parktech.sun.domain.repository.Page;
import com.parktech.sun.domain.service.downloadcenter.ResourceFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.io.IOException;

/**
 * User: Think
 * Date: 11-1-24
 * Time: 上午12:56
 */
@Controller
@RequestMapping("/management/resourcefiles/types")
public class ResourceTypeController {

    private int pageSize = Constants.PAGINATION_PAGE_SIZE;

    private ResourceFileService resourceFileService;

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    @Autowired
    public ResourceTypeController(ResourceFileService resourceFileService) {
        this.resourceFileService = resourceFileService;
    }

    @RequestMapping(value = "/search", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView search(ResourceFileSearchForm resourceFileSearchForm) {

        Page page = new Page(pageSize, resourceFileSearchForm.getPageNo());

        ModelAndView modelAndView = new ModelAndView("management/resourcefile/resourceTypeSearch");

        return modelAndView.addObject(resourceFileService.searchType(resourceFileSearchForm.getName(), page));
    }

    @RequestMapping(value = "/search", method = RequestMethod.DELETE)
    public String delete(@RequestParam("resourceTypeIds") Long[] resourceFileIds) {
        resourceFileService.deleteAll(ResourceType.class, resourceFileIds);
        return "redirect:search";
    }

    @RequestMapping(value = "/{resourceTypeId}", method = RequestMethod.GET)
    public String edit(@PathVariable Long resourceTypeId, Model model) {
        ResourceType resourceType = resourceFileService.getTypeById(resourceTypeId);
        model.addAttribute(resourceType);
        return "management/resourcefile/resourceTypeForm";
    }

    @RequestMapping(method = RequestMethod.GET)
    public String create(Model model) {
        model.addAttribute(new ResourceType());
        return "management/resourcefile/resourceTypeForm";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String save(@Valid ResourceType resourceType, BindingResult result) throws IOException {

        if (result.hasErrors()) {
            return "management/resourcefile/resourceTypeForm";
        }
        resourceFileService.saveOrUpdateType(resourceType);

        return "redirect:/management/resourcefiles/types/search";
    }
}
